<?php
		$link=mysql_connect("localhost", "root", "");
		mysql_select_db("cms");
		mysql_query("SET CHARACTER SET utf8");
		$table = strToLower("standard_treeitem");

$restructure = new restructure;
$restructure->loadTree($table);
$restructure->makeTraversed();
//echo $restructure->tree_rows[0];

class restructure {
  function restructure() {
    $this->id_name="id";
    $this->p_id_name="parent_id";
    $this->lft_name="lft";
    $this->rgt_name="rgt";
  }
  function loadTree($treename) {
    $this->treename=$treename;
    if (!@$res = mysql_query("SELECT ".$this->id_name.",".$this->p_id_name." FROM ".$treename." ORDER BY ".$this->lft_name)) {
      echo mysql_error();
    }
    while ($row = mysql_fetch_array ($res)) { $this->tree_rows[$row[$this->p_id_name]][]=$row[$this->id_name]; }
    //echo mysql_error();
    //print_r($this->tree_rows);
  }
  function makeTraversed() {
    echo "fixed ".(($this->countLR(NULL,1)-1)/2)." rows (normally everyone is fixed)";
  }
  function countLR($id_p,$i) {
    if (is_array($this->tree_rows[$id_p])) {
      foreach ($this->tree_rows[$id_p] as $ID) {
        $right=$this->countLR($ID,($i+1));
        $q="UPDATE `".$this->treename."` SET `".$this->lft_name."`=$i,`".$this->rgt_name."`=$right WHERE ".$this->id_name."=".$ID." ";
        mysql_query($q) or die($q."<br><br>".mysql_error());
        $i=$right+1;
      }
    }
    return $i;
  }
}

?>